package com.zbf.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zbf.common.utils.TwitterIdWorker;
import com.zbf.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.*;

/**
 * 作者: LCG
 * 日期: 2020/6/16 15:14
 * 描述:
 */

@Component
public class UserService {

    @Autowired
    private UserDao userDao;

    private TwitterIdWorker twitterIdWorker=new TwitterIdWorker();

    @Transactional(rollbackFor = Exception.class)
    public void insertRoleUser(Map<String, Object> maps){
        Object userId = maps.get("userId");
        //删除原有的绑定关系
        userDao.deleteRoleUser(userId.toString());
        //转为数组
        List<Long> list = JSONArray.parseArray(JSON.toJSONString(maps.get("roleIds")), Long.class);
        Map<String,Object> mm=null;
        for (Long longId : list) {
            mm=new HashMap<>();
            mm.put("id",twitterIdWorker.nextId());
            mm.put("userId",userId);
            mm.put("roleId",longId);
            userDao.insertRoleUser(mm);
        }
    }

}
